import bookApi from '@/api/book'

const user = {
  state: {
    shopCart: []
  },

  mutations: {
    addCart(state, params) {
      const isAdded = state.shopCart.find(item => item.id === params.id)
      if (isAdded) {
        isAdded.number++
      } else {
        state.shopCart.push({
          id: params.id,
          number: 1,
          picture: params.picture,
          name: params.name,
          price: params.price
        })
      }
    },
    // 删除购物车中的某本书
    deleteCart(state, id) {
      state.shopCart = state.shopCart.filter(t => t.id !== id)
    },
    clearCart(state) {
      state.shopCart = []
    }
  },

  actions: {
    // 添加到购物车
    addCart(state, id) {
      const isAdded = state.shopCart.find(item => item.id === id)
      if (isAdded) {
        isAdded.number++
      } else {
        state.shopCart.push({
          id: id,
          number: 1
        })
      }
    }
    // // 登录
    // Login({ commit }, userInfo) {
    //   const username = userInfo.username.trim()
    //   return new Promise((resolve, reject) => {
    //     login(username, userInfo.password).then(response => {
    //       const data = response.data
    //       commit('SET_NAME', data.username)
    //       commit('SET_ROLE', data.role)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // }
  }
}

export default user
